package com.admission.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.admission.dao.CollegeDetailDao;
import com.admission.model.CollegeDetail;
import com.admission.model.CollegeForm;
import com.admission.util.DeleteFile;
import com.admission.util.EnrollUtils;
import com.admission.util.ExcelUtils;
import com.admission.util.XmlUtils;

@Repository
@Transactional
public class CollegeDetailManager {
	private CollegeDetailDao collegeDetailDao;
	private CollegeFormManager collegeFormManager;

	/**
	 * 通过id查找唯一对象
	 * 
	 * @param id
	 * @return
	 */
	public CollegeDetail getById(String id) {
		return collegeDetailDao.findUniqueBy("id", id);
	}

	/**
	 * 保存
	 * 
	 * @throws IOException
	 */
	public void save(String excelPath, String CollegeId) throws Exception {
		Workbook workbook = ExcelUtils.getCommonWorkbook(excelPath);
		Sheet sheet = workbook.getSheetAt(0);
		CollegeForm c = collegeFormManager.getById(CollegeId);
		for (int j = 1; j <= sheet.getLastRowNum(); j++) {
			CollegeDetail rd = new CollegeDetail();
			Row row1 = sheet.getRow(j);
			rd.setSequence(EnrollUtils.getCellFormatValue(row1.getCell(0)));
			rd.setTicketNum(EnrollUtils.getCellFormatValue(row1.getCell(1)));
			rd.setIdNum(EnrollUtils.getCellFormatValue(row1.getCell(2)));
			rd.setName(EnrollUtils.getCellFormatValue(row1.getCell(3)));
			rd.setMajor(EnrollUtils.getCellFormatValue(row1.getCell(4)));
			rd.setAcademy(EnrollUtils.getCellFormatValue(row1.getCell(5)));
			rd.setEms(EnrollUtils.getCellFormatValue(row1.getCell(6)));
			rd.setContent(EnrollUtils.getCellFormatValue(row1.getCell(7)));
			rd.setCollegeForm(c);
			collegeDetailDao.save(rd);
			try {
				XmlUtils.Create(rd);
			} catch (Exception e) {
				throw new Exception("高考成绩页面生成失败，可能数据库中存在不合法字段，具体情况，请联系开发人员调试"
						+ e.toString());
			}
		}
		// DeleteFile.delete(excelPath);
	}

	/**
	 * 
	 * 根据collegeFrom id批量删除
	 * 
	 * @param ids
	 */
	public void deleteByIds(String id) {
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/WEB-INF/xml");
		List<CollegeDetail> l = collegeDetailDao.findBy("collegeForm.id", id);
		for (CollegeDetail c : l) {
			String regx = "collegedetail-" + c.getIdNum() + c.getTicketNum()
					+ ".xml";
			File file = new File(serverrealPath + File.separator + regx);
			DeleteFile.deleteByFile(file);
			collegeDetailDao.delete(c);
		}
	}

	public CollegeDetailDao getCollegeDetailDao() {
		return collegeDetailDao;
	}

	@Autowired
	public void setCollegeDetailDao(CollegeDetailDao collegeDetailDao) {
		this.collegeDetailDao = collegeDetailDao;
	}

	public CollegeFormManager getCollegeFormManager() {
		return collegeFormManager;
	}

	@Autowired
	public void setCollegeFormManager(CollegeFormManager collegeFormManager) {
		this.collegeFormManager = collegeFormManager;
	}
}
